# coding=utf-8
import subprocess


def shell(cmd, check_flag=True):
    """
    https://blog.csdn.net/qq_38648652/article/details/89568842
    """
    print("execute: %s" % cmd)
    p = subprocess.Popen(cmd, shell=True)
    code = p.wait()
    if check_flag and code != 0:
        print("sh: %s" % cmd)
        raise RuntimeError("execute: %s ERROR" % cmd)
    return code


def shell_v2(cmd, with_output, check_flag=True):
    print("execute: %s" % cmd)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    code = p.wait()
    if check_flag and code != 0:
        print("sh: %s" % cmd)
        raise RuntimeError("execute: %s ERROR" % cmd)
    if with_output:
        return p.communicate()
    return None, None
